/**
  ******************************************************************************
  * @file           : motor.h
  * @brief          : Header for esp8266_common.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *	Created by Chen Lu in 20210531
  *
  ******************************************************************************
  */

#ifndef __ESP8266_COMMON_H
#define __ESP8266_COMMON_H

#define ESP8266_ERROR	0
#define ESP8266_OK		1

#define DEFAULT_PORTNUM	8080	//Default port number
#define BUF_MAX_LENGTH	255		//buffer max length

//Basic AT commands
#define	AT				"AT"				//test AT launch
#define AT_RST			"AT+RST"			//re-launch module
#define	AT_GMR			"AT+GMR"			//view version information
#define	AT_GSLP			"AT+GSLP"			//enter deep-sleep mode
#define	ATE				"ATE"				//switch echo function
#define	AT_RESTORE		"AT+RESTORE"		//restore factory setting
#define	AT_UART			"AT+UART"			//UART configuration
#define	AT_UART_CUR		"AT+UART_CUR"		//UART current temporary configuration
#define	AT_UART_DEF		"AT+UART_DEF"		//UART default configuration(save to flash)
#define	AT_SLEEP		"AT+SLEEP"			//set sleep mode
#define	AT_WAKEUPGPIO	"AT+WAKEUPGPIO"		//set GPIO wake light-sleep mode
#define	AT_RFPOWER		"AT+RFPOWER"		//set RF TX power up-limitation
#define	AT_RFVDD		"AT+RFVDD"			//set RF TX power according to VDD33
#define	AT_RFAUTOTRACE	"AT+RFAUTOTRACE"	//set Frequency offset tracking
#define	AT_SYSRAM		"AT+SYSRAM"			//inquire current remain memory of the system
#define	AT_SYSADC		"AT+SYSADC"			//inquire current value of ADC
#define	AT_SYSIOSETCFG	"AT+SYSIOSETCFG"	//set IO working mode
#define	AT_SYSIOGETCFG	"AT+SYSIOGETCFG"	//inquire IO working mode
#define	AT_SYSGPIODIR	"AT+SYSGPIODIR"		//set GPIO working at input/output
#define	AT_SYSGPIOWRITE	"AT+SYSGPIOWRITE"	//set GPIO output level
#define	AT_SYSGPIOREAD	"AT+SYSGPIOREAD"	//read GPIO input level

//Basic WIFI function AT commands
#define AT_CWMODE			"AT+CWMODE"			//set WIFI mode(STA/AP/STA+AP)
#define AT_CWMODE_CUR		"AT+CWMODE_CUR"		//set WIFI mode(STA/AP/STA+AP), don't save to flash
#define AT_CWMODE_DEF		"AT+CWMODE_DEF"		//set WIFI mode(STA/AP/STA+AP), save to flash
#define AT_CWJAP			"AT+CWJAP"			//join access point
#define AT_CWJAP_CUR		"AT+CWJAP"			//join access point, don't save to flash
#define AT_CWJAP_DEF		"AT+CWJAP"			//join access point, save to flash
#define AT_CWLAPOPT			"AT+CWLAPOPT"		//set poverty of the command "AT+CWLAP"
#define AT_CWLAP			"AT+CWLAP"			//scan and list the information of access points nearby
#define AT_CWQAP			"AT+CWQAP"			//quit the access point connected
#define AT_CWSAP			"AT+CWSAP"			//set ESP8266 SoftAP configuration
#define AT_CWSAP_CUR		"AT+CWSAP_CUR"		//set ESP8266 SoftAP configuration, don't save to flash
#define AT_CWSAP_DEF		"AT+CWSAP_DEF" 		//set ESP8266 SoftAP configuration, save to flash
#define AT_CWLIF			"AT+CWLIF"			//get information of the station connect to ESP8266 SoftAP
#define AT_CWDHCP			"AT+CWDHCP" 		//set DHCP
#define AT_CWDHCP_CUR		"AT+CWDHCP_CUR" 	//set DHCP, don't save to flash
#define AT_CWDHCP_DEF		"AT+CWDHCP_DEF" 	//set DHCP, save to flash
#define AT_CWDHCPS_CUR		"AT+CWDHCPS_CUR" 	//set ESP8266 SoftAP IP range of DHCP, don't save to flash
#define AT_CWDHCPS_DEF		"AT+CWDHCPS_DEF" 	//set ESP8266 SoftAP IP range of DHCP, save to flash
#define AT_CWAUTOCONN		"AT+CWAUTOCONN" 	//set whether auto-connect AP when launched
#define AT_CIPSTAMAC		"AT+CIPSTAMAC" 		//set MAC address of ESP8266 Station
#define AT_CIPSTAMAC_CUR	"AT+CIPSTAMAC_CUR" 	//set MAC address of ESP8266 Station, don't save to flash
#define AT_CIPSTAMAC_DEF	"AT+CIPSTAMAC_DEF" 	//set MAC address of ESP8266 Station, save to flash
#define AT_CIPAPMAC			"AT+CIPAPMAC" 		//set MAC address of ESP8266 SoftAP
#define AT_CIPAPMAC_CUR		"AT+CIPAPMAC_CUR" 	//set MAC address of ESP8266 SoftAP, don't save to flash
#define AT_CIPAPMAC_DEF		"AT+CIPAPMAC_DEF" 	//set MAC address of ESP8266 SoftAP, save to flash
#define AT_CIPSTA			"AT+CIPSTA" 		//set IP address of ESP8266 Station
#define AT_CIPSTA_CUR		"AT+CIPSTA_CUR" 	//set IP address of ESP8266 Station, don't save to flash
#define AT_CIPSTA_DEF		"AT+CIPSTA_DEF" 	//set IP address of ESP8266 Station, save to flash
#define AT_CIPAP			"AT+CIPAP" 			//set IP address of ESP8266 SoftAP
#define AT_CIPAP_CUR		"AT+CIPAP_CUR" 		//set IP address of ESP8266 SoftAP, don't save to flash
#define AT_CIPAP_DEF		"AT+CIPAP_DEF" 		//set IP address of ESP8266 SoftAP, save to flash
#define AT_CWSTARTSMART		"AT+CWSTARTSMART" 	//start SmartConfig
#define AT_CWSTOPSMART		"AT+CWSTOPSMART" 	//stop SmartConfig
#define AT_CWSTARTDISCOVER	"AT+CWSTARTDISCOVER"//enable a mode that can be detected by WeChat in LAN
#define AT_CWSTOPDISCOVER	"AT+CWSTOPDISCOVER" //disable a mode that can be detected by WeChat in LAN
#define AT_WPS				"AT+WPS" 			//set WPS function
#define AT_MDNS				"AT+MDNS" 			//set MDNS function
#define AT_CWHOSTNAME		"AT+CWHOSTNAME" 	//set HOSTNAME of ESP8266 Station

//TCP/IP commands
#define AT_CIPSTATUS		"AT+CIPSTATUS" 			//inquire network connection information
#define AT_CIPDOMAIN		"AT+CIPDOMAIN" 			//DNS function
#define AT_CIPSTART			"AT+CIPSTART" 			//set up TCP connection, UDP transmission or SSL connection
#define AT_CIPSSLSIZE		"AT+CIPSSLSIZE" 		//set size of SSL buffer
#define AT_CIPSEND			"AT+CIPSEND" 			//send data
#define AT_CIPSENDEX		"AT+CIPSENDEX" 			//send data, reach the length set or come across \0, start transmitting
#define AT_CIPSENDBUF		"AT+CIPSENDBUF" 		//data written into TCP TX buffer
#define AT_CIPBUFRESET		"AT+CIPBUFRESET" 		//reset counting(TCP transmission package cache)
#define AT_CIPBUFSTATUS		"AT+CIPBUFSTATUS" 		//inquire the state of TCP transmission package cache
#define AT_CIPCHECKSEQ		"AT+CIPCHECKSEQ" 		//inquire a package in TCP package cache transmitted successfully or not
#define AT_CIPCLOSE			"AT+CIPCLOSE" 			//close TCP/UDP/SSL transmission
#define AT_CIFSR			"AT+CIFSR" 				//inquire local IP address
#define AT_CIPMUX			"AT+CIPMUX" 			//set multiply-connection mode
#define AT_CIPSERVER		"AT+CIPSERVER" 			//set TCP server
#define AT_CIPMODE			"AT+CIPMODE" 			//set unvarnished transmission mode
#define AT_SAVETRANSLINK	"AT+SAVETRANSLINK" 		//save unvarnished connection to flash
#define AT_CIPSTO			"AT+CIPSTO" 			//set timeout when ESP8266 is TCP server
#define AT_PING				"AT+PING" 				//PING function
#define AT_CIUPDATE			"AT+CIUPDATE" 			//update software through WIFI
#define AT_CIPDINFO			"AT+CIPDINFO" 			//when receive data from net, +IPD implies the IP&port(opposite end) or not
#define AT_CIPSNTPCFG		"AT+CIPSNTPCFG" 		//set time domain and SNTP server
#define AT_CIPSNTPTIME		"AT+CIPSNTPTIME" 		//inquire SNTP time
#define AT_CIPDNS_CUR		"AT+CIPDNS_CUR" 		//user-defined DNS server, don't save to flash
#define AT_CIPDNS_DEF		"AT+CIPDNS_DEF" 		//user-defined DNS server save to flash
#define PASS_THROUGH_QUIT	"+++"					//quit pass-through mode

//Encryption mode
#define OPEN			0
#define WEP				1
#define WPA_PSK			2
#define WPA2_PSK		3
#define WPA_WPA2_PSK	4

//Response of AT commands don't have macro definition, which could added further.

typedef struct{
	uint8_t len;
	uint8_t* p_data;
}DMARxTxStructure;

typedef struct{
	uint8_t* wifi_ssid;		//SSID of the Wifi
	uint8_t wifi_encryption;		//Encryption mode
	uint8_t* wifi_password;		//Password of the Wifi
}WifiConfigStructure;

uint8_t USART2_RX_BUF[BUF_MAX_LENGTH];
uint8_t USART2_TX_BUF[BUF_MAX_LENGTH];

DMARxTxStructure USART2_RX;
DMARxTxStructure USART2_TX;

int str_len(uint8_t* str);
void esp8266_construct(DMARxTxStructure *huartrxtx, uint8_t* buf);
int esp8266_init(void);
void esp8266_at_response(uint8_t mode);
uint8_t* esp8266_check_cmd(uint8_t *str);
uint8_t esp8266_send_cmd(uint8_t *cmd,uint16_t waittime);
uint8_t esp8266_send_data(uint8_t *data,uint16_t waittime);
uint8_t esp8266_quit_trans(void);

#endif
